import $ from 'pages/common';
import amapFile from 'libs/amap-wx.js';
App({
	onLaunch(options) {
		//$.login()
		this.setIphoneX();
		this.getnavinfo();
		if (wx.getStorageSync('cmp_uuid')) {
			this.getCampusData()
		}
	},
	setIphoneX() {
		let _self = this;
		wx.getSystemInfo({
			success: res => {
				let modelmes = res.model;
				if (res.safeArea.top > 20) { //x及以上的异形屏top为44，非异形屏为20
					_self.globalData.isIphoneX = true;
				} else {
					_self.globalData.isIphoneX = false;
				}
				wx.setStorageSync('modelmes', modelmes)
			},
		})
	},
	getnavinfo() {
    let systemInfo = wx.getSystemInfoSync();
    let pxToRpxScale = 750 / systemInfo.windowWidth; //px转换到rpx的比例
    // let fontSize = systemInfo.fontSizeSetting * pxToRpxScale; // 用户设置的字体大小
    let statuHeight = systemInfo.statusBarHeight * pxToRpxScale; //状态栏的高度
		let titleHeight = 44 * pxToRpxScale; //导航栏高度，这个一般是固定的
		this.globalData.statuHeight=statuHeight
		this.globalData.titleHeight=titleHeight
		// this.globalData.fontSize=fontSize
	},
	onShow(options) {
		const updateManager = wx.getUpdateManager()
		updateManager.onCheckForUpdate(function (res) {
			if (res.hasUpdate) {
				updateManager.onUpdateReady(function () {
					wx.showModal({
						title: '版本提示',
						content: '新版本已经下载好，开始体验吧！',
						showCancel: false,
						success(res) {
							//新版本重启
							updateManager.applyUpdate()
						}
					})
				})
				updateManager.onUpdateFailed(function () {
					//新版本下载失败
				})
			}
		})
		if (options.referrerInfo && options.referrerInfo.extraData) {
			this.globalData.extraData = options.referrerInfo.extraData
		}
	},
	getUserInfo(cb) {
		if (this.globalData.userInfo) {
			typeof cb == "function" && cb(this.globalData.userInfo)
		} else {
			//调用登录接口
			wx.login({
				success() {
					wx.getUserInfo({
						success(res) {
							this.globalData.userInfo = res.userInfo
							typeof cb == "function" && cb(this.globalData.userInfo)
						}
					})
				}
			})
		}
	},
	getCampusData() {
		$.get("/user/ins", {}, (e) => {
			let ins = e.institutions || []
			this.globalData.insList = ins
			if (ins.length > 0) {
				// 默认取第一个学校
				let ins_uuid = ins[0].institution_uuid
				ins.map((i) => {
					if (i.last_institution && i.last_institution == 'YES') {
						// 最后操作的学校
						ins_uuid = i.institution_uuid
					}
				})
				this.globalData.ins_uuid = ins_uuid
				$.get("/user/ins/campus", {
					institution_uuid: ins_uuid
				}, (e) => {
					let campus = e.campuss || []
					let campus_uuid = campus[0].campus_uuid
					campus.map((i) => {
						if (i.last_campus && i.last_campus == 'YES') {
							// 最后操作的校区
							campus_uuid = i.campus_uuid
							this.globalData.campusData = i
						}
					})
					wx.setStorageSync('cmp_uuid', campus_uuid)
				}, () => {}, false, true)
			}
		}, () => {}, false, true)
	},
	globalData: {
		extraData: {},
		getLocation: {},
		isIphoneX: false,
		getLocationFn: (callback) => {
			let Amap = new amapFile.AMapWX({
				key: '6feea372f82a1b2dd4b9d5c766304c92'
			});
			Amap.getRegeo({
				success: (res) => {
					let data = res[0]
					let globalData = $.gData()
					globalData.getLocation = data
					let regeocodeData = data.regeocodeData
					let address = regeocodeData.addressComponent
					let f_address = regeocodeData.formatted_address
					let area_text = address.province + address.city + address.district
					globalData.area = {
						text: area_text,
						code: address.adcode
					}
					globalData.address = f_address.split(area_text)[1] || data.name
					callback && callback()
				},
				fail: (info) => {
					console.log(info)
				}
			})
		},
		area: {
			text: "",
			code: ""
		},
		ins_uuid: null, // 最后操作的学校institution_uuid
		campusData: null, // 校区列表
		userInfo: null,
		isGuide: 'NO',
		insList: []
	}
})